package com.jvm;

import com.java8.inaction.Apple;

import java.lang.ref.Reference;

/**
 * @Author HBZ
 * @Date 2020/8/7 15:08
 * @Description
 */
public class CanReliveObj {
    public static CanReliveObj obj;

    /**
     * finalize方法只能调用一次
     *
     * @throws Throwable
     */
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("CanReliveObj finalize called");
        // 对象的复活
        obj = this;
    }

    public static void main(String[] args) throws InterruptedException {
        obj = new CanReliveObj();
        // obj 编程匿名对象
        obj = null;
        System.gc();
        Thread.sleep(1000);
        if (obj == null) {
            System.out.println("obj是null");
        } else {
            System.out.println("obj可用");
            System.out.println("第2次gc");
            // 继续把引用设置成null，让对象变成匿名对象
            obj = null;
            System.gc();
            Thread.sleep(1000);
            if (obj == null) {
                System.out.println("obj是null");
            } else {
                System.out.println("obj可用");
            }
        }
    }
    /**
     * 1、对象复活不能被回收；
     * 2、强引用不能回收，软引用弱引用可以被回收
     */
}
